package com.xg.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Admin
 * @date 2020/9/24 11:56
 */
public class DateUtil {


    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String STANDARD_FORMAT_YMD = "yyyy-MM-dd";
    public static final int localInstant = 86399999;

    /**
     * 字符串转时间
     *
     * @param strDate 字符串
     * @param format  时间格式，默认(yyyy-MM-dd)
     * @return
     */
    public static DateTime strToDate(String strDate, String format) {
        return DateTime.parse(strDate, DateTimeFormat.forPattern(
                StringUtils.isBlank(format) ? STANDARD_FORMAT_YMD : format)
        );
    }

    /**
     * 时间加 天数
     *
     * @param ds   时间
     * @param plus 天数
     * @return 结果
     */
    public static DateTime plus(DateTime ds, int plus) {
        return ds.plusDays(plus);
    }

    /**
     * 将 yyyy-MM-dd 格式的时间 加减 天数
     *
     * @param strDate  yyyy-MM-dd 格式的字符串时间
     * @param plusDays 加减天数
     * @return 加减结果
     */
    public static DateTime plus(String strDate, int plusDays) {
        return strToDate(strDate, null).plusDays(plusDays);
    }

    /**
     * 2020-10-14T00:00:00.000+08:00
     *
     * @return 今天零点
     */
    public static DateTime todayMin() {
        DateTime dateTime = new DateTime();
        return dateTime.withMillisOfDay(0);
    }

    /**
     * 2020-10-14T00:00:00.000+08:00
     *
     * @return 今天零点
     */
    public static String todayMin(String dateFromat) {
        DateTime dateTime = new DateTime().withMillisOfDay(0);
        if (StringUtils.isBlank(dateFromat)) {
            return dateTime.toString(STANDARD_FORMAT);
        }
        return dateTime.toString(STANDARD_FORMAT);
    }

    /**
     * 今天最后时刻|2020-10-14T23:59:59.999+08:00
     *
     * @return 2020-10-14T23:59:59.999+08:00
     */
    public static DateTime todayMax() {
        DateTime dateTime = new DateTime();
        return dateTime.withMillisOfDay(localInstant);
    }

    /**
     * 今天最后时刻|2020-10-14T23:59:59.999+08:00
     *
     * @return 2020-10-14T23:59:59.999+08:00
     */
    public static String todayMax(String dateFromat) {
        DateTime dateTime = new DateTime().withMillisOfDay(localInstant);
        if (StringUtils.isBlank(dateFromat)) {
            return dateTime.toString(STANDARD_FORMAT);
        }
        return dateTime.toString(dateFromat);
    }

    /**
     * 获取给定时间的最后时刻(2020-10-14 23:59:59)
     *
     * @param date       给定时间
     * @param dateFormat 时间格式化(默认是 yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String dateMax(Date date, String dateFormat) {
        return dateMinOrMax(date, dateFormat, localInstant);

    }

    /**
     * 获取给定时间的零点(2020-10-14 00:00:00)
     *
     * @param date       给定时间
     * @param dateFormat 时间格式化(默认是 yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String dateMin(Date date, String dateFormat) {
        return dateMinOrMax(date, dateFormat, 0);

    }

    private static String dateMinOrMax(Date date, String dateFormat, int localInstant) {
        DateTime dateTime = new DateTime(date).withMillisOfDay(localInstant);
        if (StringUtils.isBlank(dateFormat)) {
            return dateTime.toString(STANDARD_FORMAT);
        }
        return dateTime.toString(dateFormat);
    }


    /**
     * 获取过去7天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> getDays(int intervals) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = intervals -1; i >= 0; i--) {
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 时间加减天数
     */
    public static String addDate(String str_date,int days) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(str_date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,days);
        return format.format(calendar.getTime());
    }

    /**
     * 时间加减月份
     */
    public static String addMonth(String str_date,int month) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(str_date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,month);
        return format.format(calendar.getTime());
    }
}
